{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**SUMMARY:** *In the previous lab, we used the TVLA to assess our usual AES implementation. In this lab, we'll see how a separate module, cwtvla, can make this even easier.*\n",
    "\n",
    "**LEARNING OUTCOMES:**\n",
    "* Using cwtvla repeat the TVLA from the previous lab\n",
    "* Using cwtvla for additional TVLA testing"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## CWTVLA\n",
    "\n",
    "`cwtvla` has two main modules: an independent ktp/analysis module, and one designed for easy use with ChipWhisperer.\n",
    "The first can be imported as follows:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import cwtvla"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "And the second by:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import cwtvla.cw_convenience as conv"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "cw.__file__"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "`conv` here includes a function for automatically setting up a target. `\"STM32F3\"` will require a file called `AES.hex` in this directory with the firmware to load:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import chipwhisperer as cw\n",
    "scope, target = conv.setup_device(\"CW305\", fpga_id='100t')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "target.fpga_read(0x09, 16)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "cwtvla includes its own KTP classes. They function similarly to normal ChipWhisperer KTPs, with the major difference being that they include `next_group_A()` and `next_group_B()` methods for getting key text pairs for both groups whenever you want.\n",
    "\n",
    "**NOTE:** RAMBUS's TVLA document suggests that capture of the two groups be randomly intersperced; however, the original TVLA specification only specifies that they should be intersperced. The following uses the latter spec."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "ktp = cwtvla.FixedVRandomText()\n",
    "import chipwhisperer as cw\n",
    "import numpy as np\n",
    "from tqdm.autonotebook import trange\n",
    "N = 5000\n",
    "groupA = np.zeros((N, scope.adc.samples), dtype='float64')\n",
    "groupB = np.zeros((N, scope.adc.samples), dtype='float64')\n",
    "\n",
    "for i in trange(N):\n",
    "    key, text = ktp.next_group_A()\n",
    "    trace = cw.capture_trace(scope, target, text, key)\n",
    "    groupA[i,:] = trace.wave[:]\n",
    "    \n",
    "    key, text = ktp.next_group_B()\n",
    "    trace = cw.capture_trace(scope, target, text, key)\n",
    "    groupB[i, :] = trace.wave[:]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import chipwhisperer as cw\n",
    "trace = cw.capture_trace(scope, target, text, key)\n",
    "print(trace)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "group1, group2 = conv.capture_non_specific(scope, target, cwtvla.FixedVRandomText, N=1000)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "cw.plot(scope.get_last_trace(), label='test')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We can then run the T-Test as follows:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "t_val = cwtvla.t_test(group1, group2)\n",
    "cw.plot(scope.get_last_trace()*100) * cw.plot(t_val[0]) * cw.plot(t_val[1])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "cwtvla can also be used to report where the test failed at. For our software implementation, this will be many locations:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "fail_points = cwtvla.check_t_test(t_val)\n",
    "print(fail_points)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "`conv` also includes a function to automate collection of the TVLA data. We can use it as follows, using the fixed vs. random key test instead of the fixed vs. random text:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "group1, group2 = conv.capture_non_specific(scope, target, cwtvla.FixedVRandomKey, N=2000)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "t_val = cwtvla.t_test(group1, group2)\n",
    "fail_points = cwtvla.check_t_test(t_val)\n",
    "cw.plot(scope.get_last_trace()*60) * cw.plot(t_val[0]) * cw.plot(t_val[1]) "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "if len(fail_points) > 0:\n",
    "    print(\"Test failed at {}\".format(fail_points))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "You should see much different results for this TVLA, though it still easily fails.\n",
    "\n",
    "Let's look at the last non-specific TVLA included with `cwtvla`, semi fixed vs. random text:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "group1, group2 = conv.capture_non_specific(scope, target, cwtvla.SemiFixedVRandomText, N=10000)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "t_val = cwtvla.t_test(group1, group2)\n",
    "fail_points = cwtvla.check_t_test(t_val)\n",
    "cw.plot(scope.get_last_trace()*60) * cw.plot(t_val[0]) * cw.plot(t_val[1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
